package com.zline.foodraise.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zline.foodraise.R;
import com.zline.foodraise.entity.PictureBean;
import com.zline.foodraise.listener.PhotoViewListener;
import com.zline.foodraise.util.ImageUtil;
import com.zline.foodraise.util.LUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EBean;

import java.util.List;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by heshantao on 15/8/7.
 */
@EBean
public class PicZoomViewPageAdapter extends PagerAdapter {
    private static final String LOG_TAG=PicZoomViewPageAdapter.class.getSimpleName();
    List<PictureBean> list;
    PhotoViewListener photoViewListener;
    DisplayImageOptions options;
    ImageLoader imageLoader;
    ImageUtil imageUtil;

    public void setPicList(List<PictureBean> picList){
        this.list=picList;
    }
    //PhotoView 点击监听
    public void setPhotoViewListener(PhotoViewListener listener){
            this.photoViewListener=listener;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(container.getContext());
        imageLoader = ImageLoader.getInstance();
        imageUtil=new ImageUtil();
        options = imageUtil.
                getDisplayImageOptions(R.mipmap.default_product_icon,
                        R.mipmap.default_product_icon, R.mipmap.default_product_icon);
        String picUrl=list.get(position).getPictureUrl();
        imageLoader.displayImage(picUrl, photoView, options);
        // Now just add PhotoView to ViewPager and return it
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        photoView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener() {
            @Override
            public void onViewTap(View view, float v, float v1) {
                LUtil.i(LOG_TAG,"_________点击________");
                photoViewListener.onViewTap();
            }
        });

        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}
